package com.ransj.iphone.view;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.CheckBox;
import android.widget.Toast;

import com.ransj.iphone.IKey;
import com.ransj.iphone.IKeyListener;
import com.ransj.iphone.R;
import com.ransj.iphone.utils.IFlashLight;
import com.ransj.iphone.utils.ILog;
import com.ransj.iphone.utils.IScreenShot;
import com.ransj.iphone.utils.IUtils;

public class FunctionsPad extends AbsView implements OnClickListener {
	private static final String TAG = FunctionsPad.class.getSimpleName();
	private static final int MAX_FUNCTIONS_NUM = 8;
	private static final String TAG_SHOT = "tag_shot";
	private static final String TAG_FLASHLIGTH = "tag_flashlight";
	private static final String TAG_WAIT = "tag_wait";
	private Context mContext;
	private View m_vDevice;
	private View m_vBack;
	private CheckBox[] m_cbs;
	private int mFunctionsNum;
	private boolean mIsScreenShotEnable;
	private boolean mIsFlightLightEnable;

	public FunctionsPad(Context context, LayoutParams params,
			IKeyListener listener) {
		mContext = context;
		mKeyListener = listener;
		mWMParams = params;
		init();
	}

	private void init() {
		mMainView = LayoutInflater.from(mContext).inflate(
				R.layout.iphone_function_pad, null);
		m_vDevice = mMainView.findViewById(R.id.function_pad);
		m_vDevice.setOnClickListener(this);
		m_cbs = new CheckBox[MAX_FUNCTIONS_NUM];
		m_cbs[0] = (CheckBox) mMainView.findViewById(R.id.function_pad_item_0);
		m_cbs[0].setOnClickListener(this);
		m_cbs[1] = (CheckBox) mMainView.findViewById(R.id.function_pad_item_1);
		m_cbs[1].setOnClickListener(this);
		m_cbs[2] = (CheckBox) mMainView.findViewById(R.id.function_pad_item_2);
		m_cbs[2].setOnClickListener(this);
		m_cbs[3] = (CheckBox) mMainView.findViewById(R.id.function_pad_item_3);
		m_cbs[3].setOnClickListener(this);
		m_vBack = mMainView.findViewById(R.id.function_pad_item_4);
		m_vBack.setOnClickListener(this);
		m_cbs[4] = (CheckBox) mMainView.findViewById(R.id.function_pad_item_5);
		m_cbs[4].setOnClickListener(this);
		m_cbs[5] = (CheckBox) mMainView.findViewById(R.id.function_pad_item_6);
		m_cbs[5].setOnClickListener(this);
		m_cbs[6] = (CheckBox) mMainView.findViewById(R.id.function_pad_item_7);
		m_cbs[6].setOnClickListener(this);
		m_cbs[7] = (CheckBox) mMainView.findViewById(R.id.function_pad_item_8);
		m_cbs[7].setOnClickListener(this);
		updateStates();
	}

	public void updateStates() {
		// reset
		mFunctionsNum = 0;
		// screen shot
		mIsScreenShotEnable = IScreenShot.isFrameBufferAviliable();
		if (mIsScreenShotEnable) {
			m_cbs[mFunctionsNum]
					.setBackgroundResource(R.drawable.selector_bg_key_capture);
			m_cbs[mFunctionsNum].setTag(TAG_SHOT);
			mFunctionsNum++;
		}
		// flight light
		mIsFlightLightEnable = IUtils.isFlashLightEnable(mContext);
		if (mIsFlightLightEnable) {
			boolean isFlashLightOn = IFlashLight.getInstance().isFlashLightOn();
			m_cbs[mFunctionsNum]
					.setBackgroundResource(R.drawable.selector_bg_device_flashlight);
			m_cbs[mFunctionsNum].setChecked(isFlashLightOn);
			m_cbs[mFunctionsNum].setTag(TAG_FLASHLIGTH);
			mFunctionsNum++;
		}
		for (int i = mFunctionsNum; i < MAX_FUNCTIONS_NUM; i++) {
			m_cbs[i].setBackgroundResource(R.drawable.selector_bg_function_add);
			m_cbs[i].setChecked(false);
			m_cbs[i].setTag(TAG_WAIT);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(m_vDevice)) {
			mKeyListener.onKeyEvent(this, IKey.KEY_PAD_OUTSIDE);
		} else if (v.equals(m_vBack)) {
			mKeyListener.onKeyEvent(this, IKey.KEY_BACK_MAIN_PAD);
		} else if(v.getTag() != null){
			onItemClick(v.getTag());
		}else{
			
		}
	}
	
	public void onItemClick(Object tag){
		ILog.getInstance().w(TAG, "tag : "+tag);
		if(tag.equals(TAG_SHOT)){
			if(mKeyListener != null){
				mKeyListener.onKeyEvent(this, IKey.KEY_CAPTURE_SCREEN);
			}
		}else if(tag.equals(TAG_FLASHLIGTH)){
			boolean isFlashLightOn = IFlashLight.getInstance().isFlashLightOn();
			ILog.getInstance().w(TAG, "isFlashLightOn : "+isFlashLightOn);
			if(isFlashLightOn){
				IFlashLight.getInstance().off();
			}else{
				IFlashLight.getInstance().on();
			}
			updateStates();
		} else if (tag.equals(TAG_WAIT)) {
			Toast.makeText(mContext, R.string.function_wait_tip,
					Toast.LENGTH_SHORT).show();
		}
	}
	
}